#include "StdAfx.h"
#include "SceneSplash.h"
#include "SceneMgr.h"
#include "SceneLogin.h"
#include "TextureDrawObject.h"

SceneSplash::SceneSplash(void)
{
}

SceneSplash::~SceneSplash(void)
{
}

bool SceneSplash::Init()
{	
	auto wnd = MainWin::GetInstance();
	window_size_callback(wnd->GetWidth(), wnd->GetHeight());
	m_gameObjList1.SetRootMatrixPtr(&m_camera.GetViewMatrix());

	m_texMgr.SetLoadDir("./Data/Logo/");
	auto texDraw = new TextureDrawObject(&m_camera, m_texMgr.GetTexture("Webzenlogo.OZJ"), &Shaders::GetInstance()->m_sTexRender);
	texDraw->SetAnchorPoint(glm::vec2(0.5f, 0.5f));
	m_gameObjList1.Add(texDraw);
	//m_gameObjects[0].push_back(texDraw);
	//auto trans = texDraw->m_transformObject.Add();
	//transRoot1->AddChild((Transform*)trans);
	return true;
}

void SceneSplash::Update()
{
	//auto s = &Shaders::GetInstance()->m_sBallOfFire;
	//auto i = glGetUniformLocation(s->m_program, "iGlobalTime");
	//static float t;
	//s->UseProgram();
	//t += FrameTime::GetInstance()->GetDeltaTime()*10;
	//glUniform1f(i, t);
	//Scene::Update();
	m_gameObjList1.Update();

	glfwSwapBuffers(MainWin::GetInstance()->GetHandle());
	SceneMgr::GetInstance()->ReplaceScene(SceneLogin::Create());

	return;


	glLoadIdentity();
	float mat[4][4];
	//glRotatef(-45, 0, 1, 0);
	//auto mat1 = glm::scale<float>(glm::vec3(.5f));
	//glScalef(0.5f, 0.5f, 0.5f);
	
	glColor3f(1, 1, 1);
	float verts[] = { 0.f, 0.5f, 1.f,
		0.f, 0.f, 1.f,
		0.5f, 0.f, 1.f,
		0.5f, 0.5f, 1.f };
	float colors[] = {1.f,0.f,0.f,
		0.f,1.f,0.f,
		0.f,0.f,1.f,
		1.f,1.f,0.f};

	glEnable(GL_DEPTH_TEST);
	glTranslatef(0, 0, -2.5);
	glPushMatrix();
	//gluLookAt(0, 0, 0, 0, 0, -116.9, 0, 1, 0);
	glTranslatef(0.55f, 0.25f, 0.f);
	static int r = 20;
	r++;
	glGetFloatv(GL_MODELVIEW_MATRIX, mat[0]);
	glRotatef(r, 0, 0, 1);
	//glTranslatef(-0.25, -0.25, 0);
	glBegin(GL_TRIANGLE_FAN);
	for (int i = 0; i < 4; i++)
	{
		auto m4 = glm::make_mat4((float*)mat);
		auto m3 = glm::mat3(m4);
		auto v3 = glm::make_vec3((float*)verts + i * 3);
		v3 = v3 * m3;
		glColor3fv(colors + i * 3);
		//glVertex3fv(verts + i * 3);
		glVertex3fv(&v3.x);
	}
	glEnd();
	glPopMatrix();

	glBegin(GL_LINES);
	glColor3f(1, 1, 1);
	glVertex3f(0, 0, 0);
	glVertex3f(1000, 0, 0);
	glVertex3f(0, 0, 0);
	glVertex3f(0, 1000, 0);

	////for (int i = 0; i < 10; i++)
	////{
	////	glVertex3f(0, 0, i*10);
	////	glVertex3f(1000, 0, i*10);
	////}

	//glColor3f(0, 1, 0);
	//glVertex3f(0, 0, 0);
	//glVertex3f(0, 1000, 0);

	//glColor3f(0, 0, 1);
	//glVertex3f(0, 0, 0);
	//glVertex3f(0, 0, 100);
	glEnd();
}

void SceneSplash::window_size_callback(int width, int height)
{
	m_viewport1.SetSize(width, height);
	m_viewport1.Use();
	auto w2 = m_viewport1.m_width / 2;
	auto h2 = m_viewport1.m_height / 2;
	m_camera.Ortho(-w2, w2, -h2, h2, 100, -100);
	//m_camera.LookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);
	//glUseProgram(Shaders::GetInstance()->m_sTexRender.m_program);
	//glUniform3f(Shaders::GetInstance()->m_sTexRender.GetUniLoc("iResolution"), g_wndSize.cx, g_wndSize.cy, g_wndSize.cx / (float)g_wndSize.cy);
	//
	//glUseProgram(0);
	//m_camera.Perspective(45, g_wndSize.cx / (float)g_wndSize.cy, 1, 5000);
	//m_camera.LookAt(1000.162552, -700.124939, 1500, 1034.162552, -711.124939, 1295.630890, 0, 1, 0);
}